package game.gamestate.title.ui;

import java.awt.event.KeyEvent;

import common.Coordinate;
import common.Log;
import common.Rectangle;

import engine.Constants;
import engine.Engine;
import engine.Globals;
import engine.filesystem.ResourceManager;
import engine.input.KeyInfo;
import engine.input.KeyboardHandler_I;
import game.entity.ui.UI_Entity;
import game.gamestate.GameState;
import game.gamestate.debugroom.DebugRoom;
import game.resource.SpriteResource;
import game.resource.SpriteSheetResource;
import game.scene.Layer;
import game.scene.Layer_E;
import game.scene.sceneobject.Container_SO;
import game.scene.sceneobject.Rectangle_SO;
import game.scene.sceneobject.SceneObject_A;
import game.scene.sceneobject.Sprite_SO;
import game.scene.sceneobject.Text_SO;
import game.spritesheet.SpriteSheet_I;

public class MainMenu extends UI_Entity implements KeyboardHandler_I {
	public MainMenu() {
		super("main_menu");
		CursorIndex = 0;
	}
	private Rectangle_SO _Backdrop;
	private Container_SO _Menu;
	private SceneObject_A _Title1;
	private SceneObject_A _Title2;
	private SceneObject_A _Cursor;
	private int CursorIndex;
	private int MaxIndex;
	private boolean Pressed_OK;
	
	@Override
	public boolean create(GameState gameState) {
		ResourceManager resources = Engine.getEngine().getResourceManager();
		SpriteResource res = (SpriteResource)resources.aquireResource("cursor");
		if(res == null) {
			_Cursor = new Rectangle_SO();
		} else {
			_Cursor = new Sprite_SO(res.getSprite());
		}
		_Cursor.setLocation(new Coordinate(0, 0));
		
		Coordinate coord = new Coordinate();
		int cHeight = _Cursor.VisRect.Height;
		coord.X = _Cursor.VisRect.Width + 4;
		coord.Y = 0;
		SpriteSheetResource ssRes = (SpriteSheetResource)resources.aquireResource("font");
		SpriteSheet_I font = null;
		if(ssRes != null) {
			font = ssRes.getSpriteSheet();
		}
		Text_SO textA = new Text_SO("Debug Room", font);
		textA.setLocation(coord);
		coord.Y += cHeight;

		Text_SO textB = new Text_SO("Edit Map", font);
		textB.setLocation(coord);
		coord.Y += cHeight;

		Text_SO textC = new Text_SO("Exit", font);
		textC.setLocation(coord);
				
		MaxIndex = 2;
		_Menu = new Container_SO();
		_Menu.add(textA);
		_Menu.add(textB);
		_Menu.add(textC);
		_Menu.add(_Cursor);
		int x = (Constants.SCREEN_WIDTH - _Menu.VisRect.Width) / 2;
		int y = (Constants.SCREEN_HEIGHT - _Menu.VisRect.Height) / 2;
		_Menu.setLocation(new Coordinate(x, y));
		
		Text_SO title = new Text_SO(Constants.GAME_NAME, font);
		x = (Constants.SCREEN_WIDTH - title.VisRect.Width) / 2;
		y -= (title.VisRect.Height * 3);
		title.setLocation(new Coordinate(x,y));
		_Title1 = title;
		
		title = new Text_SO(Constants.VERISON_STRING, font);
		x = (Constants.SCREEN_WIDTH - title.VisRect.Width) / 2;
		y += (title.VisRect.Height);
		title.setLocation(new Coordinate(x,y));
		_Title2 = title;
		
		_Backdrop = new Rectangle_SO();
		Rectangle rect = _Menu.VisRect.clone();
		rect.X -= 4;
		rect.Y -= 4;
		rect.Width += 8;
		rect.Height += 8;
		_Backdrop.setBoundry(rect);
		_Backdrop.Color = (byte)0x00;
		_Backdrop.Priority = -1;
		
		Layer layer = gameState.Scene.getLayer(Layer_E.UI);
		layer.insert(_Menu);
		layer.insert(_Title1);
		layer.insert(_Title2);
		layer.insert(_Backdrop);
		
		
		gameState.Keyboard.add(this);
		
		return true;
	}
	
	@Override
	public boolean think(GameState gameState, int elapsed) {
		if(Pressed_OK == true) {
			switch(CursorIndex) {
				case 0:		// Debug Room
					Engine.getEngine().setNextGame(new DebugRoom(gameState));
					break;
				case 1:		// Editor
					Pressed_OK = false;
					Log.debug("Not implemented.");
					break;
				case 2:		// Quit
					Globals.KillSwitch = !Globals.KillSwitch;
					break;
			}
			Pressed_OK = false;
		}
		return true;
	}
	
	@Override
	public boolean update(GameState gameState, int time) {
		SceneObject_A so = _Menu.get(CursorIndex);
		_Cursor.setLocation(new Coordinate(0, so.VisRect.Y));
		return true;
	}

	@Override
	public boolean destroy(GameState gameState) {
		Layer layer = gameState.Scene.getLayer(Layer_E.UI);
		layer.remove(_Menu);
		layer.remove(_Title1);
		layer.remove(_Title2);
		layer.remove(_Backdrop);
		gameState.Keyboard.remove(this);
		return true;
	}
	
	// KeyboardHandler_I -------------------------------------------------------

	@Override
	public boolean handleKeyboard(KeyInfo keyInfo) {
		switch(keyInfo.VKey) {
			case KeyEvent.VK_UP:
				if(keyInfo.Pressed == true) {
					CursorIndex--;
				}
				break;
			case KeyEvent.VK_DOWN:
				if(keyInfo.Pressed == true) {
					CursorIndex++;
				}
				break;
			case KeyEvent.VK_ENTER:
				Pressed_OK = keyInfo.Pressed;
				break;
			default:
				return false;
		}
		return true;
	}

}